package com.pulanit.pangu.admin.web.controller.business;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pulanit.pangu.admin.business.api.entity.Notice;
import com.pulanit.pangu.admin.business.api.service.INoticeService;
import com.pulanit.pangu.admin.business.api.param.NoticePageIn;
import cn.hutool.core.lang.Assert;
import com.gitee.pulanos.pangu.framework.sdk.dto.Result;
import com.gitee.pulanos.pangu.framework.sdk.dto.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import com.gitee.pulanos.pangu.framework.sdk.dto.page.PagingUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

/**
 * <p>
 * 公告表 前端控制器
 * </p>
 *
 * @author 天鸣
 * @since 2024-03-05
 */
@RestController
@RequestMapping("/api/business/notice")
public class NoticeController {

    @Value("${server.port}")
    private int serverPort;


    @Autowired
    private INoticeService noticeService;

    /**
     * 分页
     */
    @GetMapping("/page")
    public Result<PageResult<Notice>> list(NoticePageIn noticePageIn) {
        Page page = PagingUtil.createPage(noticePageIn);
        LambdaQueryWrapper<Notice> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotEmpty(noticePageIn.getKeyword()), Notice::getNoticeTitle, noticePageIn.getKeyword())
                .orderByDesc(Notice::getCreateTime);
        Page<Notice> resultPage = noticeService.page(page, wrapper);
        resultPage.getRecords().forEach(item -> {
            //处理文件地址
            if (StrUtil.isNotBlank(item.getFileUrl())) {
                try {
                    InetAddress address = InetAddress.getLocalHost();
                    String localPrefix = "http://" + address.getHostAddress() + ":" + Convert.toStr(serverPort);
                    String downloadFile = localPrefix + item.getFileUrl();
                    item.setDownloadFile(downloadFile);
                } catch (UnknownHostException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        return Result.success(PagingUtil.transformPageResult(resultPage));
    }

    /**
     * 获取列表
     *
     * @return
     */
    @GetMapping("/list")
    public Result<List<Notice>> list() {
        List<Notice> list = noticeService.list();
        return Result.success(list);
    }

    /**
     * 获取详情
     *
     * @return
     */
    @GetMapping("/detail")
    public Result<Notice> detail(@RequestParam("id") Long id) {
        Assert.notNull(id, "ID 不能为空");
        Notice notice = noticeService.getById(id);
        return Result.success(notice);
    }

    /**
     * 保存
     *
     * @param notice
     * @return
     */
    @PostMapping("/add")
    public Result<Void> add(@RequestBody Notice notice) {
        if (StrUtil.isNotBlank(notice.getFileUrl())) {
            notice.setNoticeType(1);
        } else {
            notice.setNoticeType(0);
        }
        notice.setCreateTime(DateTime.now().toLocalDateTime());
        noticeService.save(notice);
        return Result.success();
    }

    /**
     * 修改
     *
     * @param notice
     * @return
     */
    @PostMapping("/update")
    public Result<Void> update(@RequestBody Notice notice) {
        if (StrUtil.isNotBlank(notice.getFileUrl())) {
            notice.setNoticeType(1);
        } else {
            notice.setNoticeType(0);
        }
        noticeService.updateById(notice);
        return Result.success();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public Result<Void> delete(@RequestParam("id") Long id) {
        Assert.notNull(id, "ID 不能为空");
        noticeService.removeById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchDelete")
    public Result<Void> batchDelete(@RequestParam List<Long> ids) {
        Assert.notEmpty(ids, "ID 不能为空");
        noticeService.removeBatchByIds(ids);
        return Result.success();
    }

}
